Trò chơi Tic-Tac-Toe, game đánh caro full source code
53.606 lượt xem;
- GameLogic.cs
- MarcoPolo-Tutorial /
- Demos /
- Photon Unity Networking /
- Assets /
- project /
1 using UnityEngine;
2 using System.Collections;
3
4 public class GameLogic : MonoBehaviour
5 {
6
7 public static int playerWhoIsIt = 0;
8 private static PhotonView ScenePhotonView;
9
10 // Use this for initialization
11 public void Start()
12 {
13 ScenePhotonView = this.GetComponent<PhotonView>();
14 }
15
16 public void OnJoinedRoom()
17 {
18 // game logic: if this is the only player, we're "it"
19 if (PhotonNetwork.playerList.Length == 1)
20 {
21 playerWhoIsIt = PhotonNetwork.player.ID;
22 }
23
24 Debug.Log("playerWhoIsIt: " + playerWhoIsIt);
25 }
26
27 public void OnPhotonPlayerConnected(PhotonPlayer player)
28 {
29 Debug.Log("OnPhotonPlayerConnected: " + player);
30
31 // when new players join, we send "who's it" to let them know
32 // only one player will do this: the "master"
33
34 if (PhotonNetwork.isMasterClient)
35 {
36 TagPlayer(playerWhoIsIt);
37 }
38 }
39
40 public static void TagPlayer(int playerID)
41 {
42 Debug.Log("TagPlayer: " + playerID);
43 ScenePhotonView.RPC("TaggedPlayer", PhotonTargets.All, playerID);
44 }
45
46 [RPC]
47 public void TaggedPlayer(int playerID)
48 {
49 playerWhoIsIt = playerID;
50 Debug.Log("TaggedPlayer: " + playerID);
51 }
52
53 public void OnPhotonPlayerDisconnected(PhotonPlayer player)
54 {
55 Debug.Log("OnPhotonPlayerDisconnected: " + player);
56
57 if (PhotonNetwork.isMasterClient)
58 {
59 if (player.ID == playerWhoIsIt)
60 {
61 // if the player who left was "it", the "master" is the new "it"
62 TagPlayer(PhotonNetwork.player.ID);
63 }
64 }
65 }
66
67 public void OnMasterClientSwitched()
68 {
69 Debug.Log("OnMasterClientSwitched");
70 }
71 }
2 using System.Collections;
3
4 public class GameLogic : MonoBehaviour
5 {
6
7 public static int playerWhoIsIt = 0;
8 private static PhotonView ScenePhotonView;
9
10 // Use this for initialization
11 public void Start()
12 {
13 ScenePhotonView = this.GetComponent<PhotonView>();
14 }
15
16 public void OnJoinedRoom()
17 {
18 // game logic: if this is the only player, we're "it"
19 if (PhotonNetwork.playerList.Length == 1)
20 {
21 playerWhoIsIt = PhotonNetwork.player.ID;
22 }
23
24 Debug.Log("playerWhoIsIt: " + playerWhoIsIt);
25 }
26
27 public void OnPhotonPlayerConnected(PhotonPlayer player)
28 {
29 Debug.Log("OnPhotonPlayerConnected: " + player);
30
31 // when new players join, we send "who's it" to let them know
32 // only one player will do this: the "master"
33
34 if (PhotonNetwork.isMasterClient)
35 {
36 TagPlayer(playerWhoIsIt);
37 }
38 }
39
40 public static void TagPlayer(int playerID)
41 {
42 Debug.Log("TagPlayer: " + playerID);
43 ScenePhotonView.RPC("TaggedPlayer", PhotonTargets.All, playerID);
44 }
45
46 [RPC]
47 public void TaggedPlayer(int playerID)
48 {
49 playerWhoIsIt = playerID;
50 Debug.Log("TaggedPlayer: " + playerID);
51 }
52
53 public void OnPhotonPlayerDisconnected(PhotonPlayer player)
54 {
55 Debug.Log("OnPhotonPlayerDisconnected: " + player);
56
57 if (PhotonNetwork.isMasterClient)
58 {
59 if (player.ID == playerWhoIsIt)
60 {
61 // if the player who left was "it", the "master" is the new "it"
62 TagPlayer(PhotonNetwork.player.ID);
63 }
64 }
65 }
66
67 public void OnMasterClientSwitched()
68 {
69 Debug.Log("OnMasterClientSwitched");
70 }
71 }
Use this for initialization
game logic: if this is the only player, we're "it"
when new players join, we send "who's it" to let them know
only one player will do this: the "master"
if the player who left was "it", the "master" is the new "it"